package Client;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class PasswordGenerator {
	
	private Random rand;

	public PasswordGenerator(){
		rand = new Random();
	}
	
	
	public String newPassword(String password) {
		   try {
		        java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-1");
		        byte[] array = md.digest(password.getBytes());
		        StringBuffer sb = new StringBuffer();
		        for (int i = 0; i < array.length; ++i) {
		          sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
		       }
		        return sb.toString();
		    } catch (java.security.NoSuchAlgorithmException e) {
		    }
		    return null;
		}
	
	public String newSalt(){
		StringBuilder sb = new StringBuilder();
		int random = rand.nextInt(99999999);
		sb.append(random);
		String salt = sb.toString();
		return salt;
	}
	
	
	public String reHashPw(String password){
		 try {
		        java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-1");
		        byte[] array = md.digest(password.getBytes());
		        StringBuffer sb = new StringBuffer();
		        for (int i = 0; i < array.length; ++i) {
		          sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
		       }
		        return sb.toString();
		    } catch (java.security.NoSuchAlgorithmException e) {
		    }
		    return null;
	}
	
	public String saltPassword(String password, String salt){
		StringBuilder sb = new StringBuilder();
		password = sb.append(password + salt).toString();
		return password;
	}
	
}
